package com.bdzl.admin.service.data.impl;

import com.bdzl.pub.util.file.BASE64DecodedMultipartFile;
import com.bdzl.pub.util.file.OssUploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.bdzl.admin.dao.data.FddCaEntDao;
import com.bdzl.pub.entity.data.FddCaEntDO;
import com.bdzl.admin.service.data.FddCaEntService;
import org.springframework.web.multipart.MultipartFile;


@Service
public class FddCaEntServiceImpl implements FddCaEntService {
	@Autowired
	private FddCaEntDao fddCaEntDao;
	
	@Override
	public FddCaEntDO get(Integer pceId){
		return fddCaEntDao.get(pceId);
	}
	
	@Override
	public List<FddCaEntDO> list(Map<String, Object> map){
		return fddCaEntDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return fddCaEntDao.count(map);
	}
	
	@Override
	public int save(FddCaEntDO fddCaEnt){
		if (fddCaEnt.getPecSealPic()!=null && !fddCaEnt.getPecSealPic().startsWith("http")) {
			try {
				//上传百度云
				MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(fddCaEnt.getPecSealPic());
				String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file1, "oan/ent/ent_seal  ");
				//修改头像
				fddCaEnt.setPecSealPic(docUrl);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return fddCaEntDao.save(fddCaEnt);
	}
	
	@Override
	public int update(FddCaEntDO fddCaEnt){
		if (fddCaEnt.getPecSealPic()!=null && !fddCaEnt.getPecSealPic().startsWith("http")) {
			try {
				//上传百度云
				MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(fddCaEnt.getPecSealPic());
				String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file1, "loan/ent/ent_seal ");
				//修改头像
				fddCaEnt.setPecSealPic(docUrl);
				if (fddCaEnt.getPecSealPic() != null) {
					//获取旧头像
					String userHeadImg = fddCaEntDao.get(fddCaEnt.getPceId()).getPecSealPic();
					if (userHeadImg != null && userHeadImg.startsWith("http")) {
						OssUploadFileUtil.remove(userHeadImg);
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return fddCaEntDao.update(fddCaEnt);
	}
	
	@Override
	public int remove(Integer pceId){
		return fddCaEntDao.remove(pceId);
	}
	
	@Override
	public int batchRemove(Integer[] pceIds){
		return fddCaEntDao.batchRemove(pceIds);
	}
	
}
